package com.fudian.spark_platform.Configure

import net.minidev.json.JSONObject
import scala.collection.mutable
import scala.io.Source

class AppSettingConfig {

    var settings = mutable.Map(
        "AppName" -> "孚典Spark数据分析平台",
        "SparkMaster" -> "local[*]"
    )


    def setConf(appConf:JSONObject):AppSettingConfig = {
        val appKey = appConf.keySet()
        val iter = appKey.iterator()

        while (iter.hasNext){
            val keyStr = iter.next()
            this.settings(keyStr) = appConf.get(keyStr).toString
        }
        this
    }
}

class DataSourceInputConfig {

    var inputConfig = mutable.Map(
        "DataSourceType" -> "textFile",
        "DataSourceLocation" -> "~/Desktop/wechat_topic.csv",
        "DataSourceDelimiter" -> ",",
        //后期这个字段可以支持excel文件读取支持
        "DataSourceFileType" -> "csv",
        //uri目前暂时只用于mongodb的实现
        "DataSourceDBUri" -> "",
        //对于通用性数据库
        "DataSourceHost" -> "127.0.0.1",
        "DataSourcePort" -> "3306",
        "DataSourceUser" -> "root",
        "DataSourcePwd" -> "",
        "DataSourceDBName" -> "",
        //在mongodb中指代的是 collection
        "DataSourceDBTable" -> "",
        "DataSourceDBFds" -> Array(),
        //载入的数据量多少
        "DataSourceLimitCount" -> 0
    )

    def setConf(appConf:JSONObject):DataSourceInputConfig = {
        this
    }
}

class MillConfig {

    var millingConfig = mutable.Map(
        "ZHable" -> false,
        "AllowNumber" -> false,
        "LimitCount" -> 2,
        "FastStopWords" -> Array(""),
        "ZHStopWords" -> this.getDefaultStopWords("ZH"),
        "ENStopWords" -> this.getDefaultStopWords("EN"),
        "USStopWords" -> this.getDefaultStopWords("US")
    )


    def setConf(millConf:JSONObject):MillConfig = {
        this
    }


    /**
      * 获取统一的停用词文本词语
      *
      * @return
      */
    def getDefaultStopWords(stopType:String): Array[String] = {
        var tempArr = ""
        var resultsArr = Array("")
        stopType match {
            case "ZH" => {
                for(line <- Source.fromURL(this.getClass.getClassLoader.getResource("stopWords_zh.txt")).getLines()) {
                    tempArr = tempArr + " " + line
                }
                resultsArr = tempArr.split(" ")
            }
            case "EN" => {
                for(line <- Source.fromURL(this.getClass.getClassLoader.getResource("stopWords_en.txt")).getLines()) {
                    tempArr = tempArr + " " + line
                }
                resultsArr = tempArr.split(" ")
            }
            case "US" => {
                for(line <- Source.fromURL(this.getClass.getClassLoader.getResource("user_stop.txt")).getLines()) {
                    tempArr = tempArr + " " + line
                }
                resultsArr = tempArr.split(" ")
            }
        }
        resultsArr
    }
}

class TransformConfig {
    var transformConfig = mutable.Map(
        "TransformType" -> "CountVecotor",
        "InputCol" -> "words",
        "OutPutCol" -> "feature"
    )

    def setConf(transformConf:JSONObject):TransformConfig = {
        this
    }

}

class MLConfig {
    var mLConfig = mutable.Map(
        "MLType" -> "LDA",
        "LDA" -> mutable.Map(
            "k" -> 3,
            "maxItNum" -> 20,
            "optimizer" -> "em",
            "describeTopics" -> 10
        ),

        "TF_IDF" -> mutable.Map(
            "minDoc" -> 2
        ),

        "K_Means" -> mutable.Map(),
        "ALS" -> mutable.Map(),
        "Word2Vector" -> mutable.Map()
//        "MLParams" -> JSONObject
    )

    def setConf(mlConf:JSONObject):MLConfig = {
        this
    }
}

class OutputConfig {

    var outConfig = mutable.Map(
        "OutputType" -> "console",
        //文本输出配置
        "OutTextPath" -> "",
        //数据库数据配置
        "OutDataBaseName" -> "",
        "OutDataBaseHost" -> "",
        "OutDataBasePort" -> "",
        "OutDataBaseTable" -> "",
        "OutDataBaseFds" -> Array(),
        "OutDataBaseUser" -> "",
        "OutDataBasePwd" -> "",
        //hdfs输出配置
        "OutHDFSPath" -> ""
    )

    def setConf(outConf:JSONObject):OutputConfig = {
        this
    }

}

